package com.aimee.dao;

import com.aimee.entity.Lunbo;
import com.aimee.util.Dbutil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class LunboDAO {
    //查出所有的轮播数据（查一页数据--分页查询)
    public List show() throws SQLException {
        String sql="SELECT * FROM advertise";
        Connection conn = Dbutil.getConn();
        QueryRunner runner = new QueryRunner();
        List list=runner.query(conn, sql, new BeanListHandler<Lunbo>(Lunbo.class));
        conn.close();
        return list;
    }
    //找出轮播图地址
/*    public List find() throws SQLException {
        String sql="SELECT advertise_address FROM advertise";
        Connection conn = Dbutil.getConn();
        QueryRunner runner = new QueryRunner();
        List list=runner.query(conn, sql, new BeanListHandler<Lunbo>(Lunbo.class));
        conn.close();
        return list;
    }*/
    //添加轮播数据
    public int add(Lunbo lunbo)throws SQLException{
        String sql="INSERT INTO `advertise` VALUE(NULL,?,?)";
        Connection conn = Dbutil.getConn();
        QueryRunner runner = new QueryRunner();
        int n=0;
        Object param[]=new Object[2];
        param[0]=lunbo.getAdvertise_address();
        param[1]=lunbo.getAdvertise_size();
        n=runner.update(conn, sql,param);
        conn.close();
        return n;
    }
    //删除轮播数据
    public int del(int id) throws Exception{
        String sql="delete from advertise where advertise_id=?";
        Connection conn = Dbutil.getConn();
        QueryRunner runner = new QueryRunner();
        int n=runner.update(conn,sql,id);
        conn.close();
        System.out.println(n);
        return n;

    }


}
